<?php 

// Set Paths
$options_path  = TEMPLATEPATH . '/themeOptions/';
$functions_path= TEMPLATEPATH . '/functions/';
//$widgets_path  = TEMPLATEPATH . '/widgets/';

// Get Theme functions
require_once ($functions_path . 'menus.php'); 		 		// Define available menus
require_once ($functions_path . 'theme-comments.php'); 		// Custom comments
require_once ($functions_path . 'imageresize.php'); 		// Image Sizing using VT
require_once ($functions_path . 'sidebar-setup.php'); 		// Setup sidebars
require_once ($functions_path . 'custom-header.php');		// WordPress header function
require_once ($functions_path . 'page-metaboxes.php');  // Custom Post Type for Portfolio
require_once ($functions_path . 'posttype-portfolio.php');  // Custom Post Type for Portfolio
require_once ($functions_path . 'posttype-slideshow.php');	// Custom Post type for Sliders
require_once ($functions_path . 'popular-widget.php');  	// Popular widget with thumbnails
require_once ($functions_path . 'recent-widget.php');  		// Recent Posts with thumbnails
require_once ($functions_path . 'gp-text-widget.php');  		// Recent Posts with thumbnails


// Get Theme Settings
require_once ($options_path . 'functions.php'); 			// This loads the theme option settings

// Establish content width
if ( ! isset( $content_width ) )
	$content_width = 960;

// Load theme scripts
if ( !is_admin() ) :

// PrettyPhoto Lightbox
wp_enqueue_style('css-prettyphoto', get_template_directory_uri() . '/js/prettyphoto/css/prettyPhoto.css',false,'1.1','all');
wp_enqueue_script('scripts-prettyphoto', get_template_directory_uri() . '/js/prettyphoto/js/jquery.prettyPhoto.js',array('jquery'),'3.1.3',true);
wp_enqueue_script('nivoslider', get_bloginfo('template_url').'/js/jquery.nivo.slider.pack.js', array('jquery'));
// Kwicks Accordion Slider
//wp_enqueue_script('scripts-kwicks', get_template_directory_uri() . '/js/jquery.kwicks.min.js',array('jquery'),'2.1.2',true);
//wp_enqueue_script('kwicks-settings', get_template_directory_uri() . '/js/kwicks-settings.js',array('jquery'),'1.0',true); 
endif;

// When this theme is activated send the user to the theme options
if (is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
	// Call action that sets
	//add_action('admin_head','gp_setup');
	// Do redirect
	header( 'Location: '.admin_url().'themes.php?page=options-framework' ) ;
}

//This enables post-thumbnail support for a theme.
add_theme_support('post-thumbnails', array('post', 'page', 'portfolio', 'slideshow', 'testimonial'));


// Add support for custom backgrounds
if(function_exists('add_custom_background')) {
	add_custom_background('set_theme_background'); 
	function set_theme_background() {
		$bgimage = get_background_image();
		$bgcolor = get_background_color();
		$themeurl = get_template_directory_uri();
		echo "<style type='text/css'>\n";
		if(!empty($bgimage)) {
			$background_styles = 'background-image: url(\'' . get_theme_mod('background_image', '') . '\');'
			. ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
			. ' background-position: top ' . get_theme_mod('background_position_x', 'left') .  ';' . 'background-attachment: '. get_theme_mod('background_attachment', 'scroll');
			echo "body { ".$background_styles."); } \n";
		} 
		if(!empty($bgcolor)) {
			echo "body { background-color: #".$bgcolor."; }; \n";
		}

		echo "</style>";
	}
}	

// Add custom css to the editor
add_editor_style();	

// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );

// Remove the p tag from category descriptions
remove_filter('term_description','wpautop');

// Standard Excerpt length
function gp_excerpt_length($length) {
	return 60;
}
add_filter('excerpt_length', 'gp_excerpt_length');

// Special excerpt length per instance ie showcase column excerpts
function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."...";
echo $excerpt;
}

// change the excerpt ending 
function new_excerpt_more($more) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

// Remove styles when the gallery shortcode is used.
function remove_gallery_css( $css ) {
	return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}
add_filter( 'gallery_style', 'remove_gallery_css' );

// Make this theme language capable
load_theme_textdomain( 'gp', get_template_directory() . '/languages' );

	$locale = get_locale();
	$locale_file = get_template_directory() . "/languages/$locale.php";
	if ( is_readable( $locale_file ) )
		require_once( $locale_file );

// Stops WordPress from going to middle of full post view - very irrating. Thanks to http://digwp.com
function remove_more_jump_link($link) { 
	$offset = strpos($link, '#more-');
	if ($offset) {
		$end = strpos($link, '"',$offset);
	}
	if ($end) {
		$link = substr_replace($link, '', $offset, $end-$offset);
	}
	return $link;
}
add_filter('the_content_more_link', 'remove_more_jump_link');

/** 
 * Custom WordPress Search Results
 * Default wordpress search will search everything so this keeps a cleaner results page with posts only
 * Credit to www.bavotasan.com
 **/

function SearchFilter($query) {
if ($query->is_search) {
    $query->set('post_type', 'post');
}
    return $query;
}
add_filter('pre_get_posts','SearchFilter');


// lets remove that irritating tags list after the comment form
function mytheme_init() {
	add_filter('comment_form_defaults','mytheme_comments_form_defaults');
}
add_action('after_setup_theme','mytheme_init');

function mytheme_comments_form_defaults($default) {
	unset($default['comment_notes_after']);
	return $default;
}

/**
 * Display navigation to next/previous pages when applicable
 */
function gp_content_nav( $nav_id ) {
	global $wp_query;

	if ( $wp_query->max_num_pages > 1 ) : ?>
		<nav id="<?php echo $nav_id; ?>">
			<h4 class="assistive-text"><?php _e( 'More Articles', 'gp' ); ?></h4>
			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&raquo;</span> Next Page', 'gp' ) ); ?></div>
			<div class="nav-next"><?php previous_posts_link( __( '<span class="meta-nav">&laquo;</span> Previous Page ', 'gp' ) ); ?></div>
		</nav><!-- #nav-above -->
	<?php endif;
}

// Menu Fix for CPT
function remove_parent($var)
{
	// check for current page values, return false if they exist.
	if ($var == 'current_page_item' || $var == 'current_page_parent' || $var == 'current_page_ancestor'  || $var == 'current-menu-item') { return false; }

	return true;
}

function add_class_to_cpt_menu($classes)
{
	// your custom post type name
	if (get_post_type() == 'portfolio')
	{
		// we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items.
		$classes = array_filter($classes, "remove_parent");

		// add the current page class to a specific menu item.
		if (in_array('menu-item-17', $classes)) $classes[] = 'current_page_parent';
	}
	return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_cpt_menu');



/**
* Added By Brian
* 
* @param mixed $args
*/
function generate_dropdown($args = '') {

    // fill in the defaults
    $defaults = array(
        'values' => array(),
        'echo'     => 1,
        'name'     => 'generic_select',
        'onChange' => '',
        'selected' => 0,
        'size'     => -1,
        'style'    => 'width:100%',
    );
    $r = wp_parse_args($args, $defaults );
    extract( $r, EXTR_SKIP );

    $output  = '<select id="'.$name.'" name="'.$name.'" ';

    if ($size == -1)
        $output .= 'size="'.count($counties).'" ';
    else if ($size > 0 )
      $output .= 'size="'.$size.'" ';
      
    $output .= 'style="$style" '.$onChange.'>';

    if (! is_array($values)) return;
    foreach ( $values as $key => $value) {
        
        $strSelected = ( $selected == $key ? 'selected="selected"':'' );

        $output .= '<option '.$strSelected.' value="'.$key.'">'.$value.'</option>';
    }

    $output .= "</select>\n";

    if ($echo)
        echo $output;
    else
        return $output;
}


add_action( 'after_setup_theme', 'cfa_setup' );
if ( ! function_exists( 'cfa_setup' ) ):

function cfa_setup() {
	if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
		add_image_size( 'slider-post-thumbnail', 1100, 380, true ); // Slider Thumbnail
	}
}

endif;
